package com.bob.clan.wxconnector.wxService;

import com.alibaba.fastjson.JSONObject;
import com.bob.clan.wxconnector.component.RestApiComponent;
import com.bob.clan.wxconnector.config.WxServerConfig;
import com.bob.clan.wxconnector.exception.MpException;
import com.bob.clan.wxconnector.util.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信参数二维码服务
 */
@Slf4j
@Service
public class QrService {
    @Autowired
    private WxServerConfig wxServerConfig;
    @Autowired
    private TokenService tokenService;


    /**
     * 获取永久字符串参数二维码
     * @param sysCode
     * @param scene
     * @return 返回值为不是二维码的地址，可以当前String,在HTML转为Img显示
     * @throws JsonProcessingException
     */
    public String getArgumentQr(String sysCode, String scene) throws JsonProcessingException {
        String accessToken = tokenService.getToken(sysCode);
        String qrUrl = wxServerConfig.getUrlByToken(wxServerConfig.getAccessTokenUrl(), accessToken);
        //组装数据
        JSONObject actionJson = new JSONObject();
        JSONObject paramJson = new JSONObject();
        JSONObject sceneJson = new JSONObject();
        actionJson.put("action_name", "QR_LIMIT_STR_SCENE");//临时
        sceneJson.put("scene_str", scene);
        paramJson.put("scene", sceneJson);
        actionJson.put("action_info", paramJson);
        log.info("QR json=" + actionJson);
        JSONObject retJson = HttpUtil.doPostSSL(qrUrl, actionJson);
        log.info("wechat back json=" + retJson);
        return retJson.getString("url");
    }

}
